package com.sgb.kjwl.view.widget;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;

import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.PopwinowTeamWorkersBinding;
import com.sgb.kjwl.model.entity.EmploymentEntity;
import com.sgb.kjwl.view.widget.pick.MapAppWindow;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.util.DateUtils;

/**
 * @author: 王彬臣
 * @date: 2021/8/19
 * @Description: 用工需求(临时用工)图查PopWindow
 */
public class MapTeamWorkPopWindow extends BasePopWindow implements View.OnClickListener {
    private static final int MIN_CLICK_DELAY_TIME = 2000;
    private static long lastClickTime;
    PopwinowTeamWorkersBinding mBinding;
    BtnClickListener clickListener;
    EmploymentEntity mEntity;
    View rootView;

    public void setRootView(View rootView) {
        this.rootView = rootView;
    }

    public void setClickListener(BtnClickListener clickListener) {
        this.clickListener = clickListener;
    }

    public MapTeamWorkPopWindow(Context context) {
        super(context, DIR_DOWN_UP);
    }

    @Override
    protected int popLayout() {
        return R.layout.popwinow_team_workers;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        mBinding = (PopwinowTeamWorkersBinding) holder.getmBinding();
        mBinding.setOnClick(this);

    }

    public void setData(EmploymentEntity entity) {
        mEntity = entity;
        mBinding.tvTitle.setText(String.format("%s%s%s",entity.getWorkTypeName().replace("、","/"), TextUtils.isEmpty(entity.getEmploymentDescription()) ? "" : "-" , entity.getEmploymentDescription()));
        mBinding.tvTime.setText(String.format("发布时间：%s", DateUtils.timestampTo13Date3(entity.getCreateTime())));
        mBinding.tvWorkerType.setText(entity.getWorkTypeName().replace("、","|"));
        switch (entity.getSalaryType()) {
            case 1:
                mBinding.tvMoney.setText("面议");
                break;
            case 2:
                mBinding.tvMoney.setText("电议");
                break;
            case 3:
                String salary = entity.getSalary();
                if (entity.getSalary().startsWith("-") || entity.getSalary().endsWith("-")) {
                    salary = entity.getSalary().replace("-", "");
                }
                mBinding.tvMoney.setText(salary + "元/" + entity.getSalaryUnitName());
                break;
            default:
                break;
        }
        mBinding.tvDemandInfo.setText(entity.getIndustryName() + "/" + entity.getCategoryName());
        mBinding.tvAddressInfo.setText(entity.getAddressAll());
        mBinding.tvLinkName.setText(entity.getContact());
        mBinding.tvLinkTel.setText(Html.fromHtml("联系人：<font color='#5792FD'>" + entity.getContactPhone() + "</font>"));
        Resources res = mContext.getResources();
        Drawable imgs;

        if (mEntity.isFocus()) {
            mBinding.tvFollow.setText("已关注");
            mBinding.tvFollow.setTextColor(mContext.getResources().getColor(R.color.color_ef4033));
            imgs = res.getDrawable(R.mipmap.icon_red_follow);

        } else {
            mBinding.tvFollow.setText("关注");
            mBinding.tvFollow.setTextColor(mContext.getResources().getColor(R.color.color_tab_two));
            imgs = res.getDrawable(R.mipmap.icon_follow);
        }
        imgs.setBounds(0, 0, imgs.getMinimumWidth(), imgs.getMinimumHeight());
        mBinding.tvFollow.setCompoundDrawables(null, imgs, null, null);

        if (mEntity.isApply()){
            mBinding.tvApply.setText("已报名");
            mBinding.tvApply.setBackgroundResource(R.drawable.red_color_change_radius_5_alpha_0_5);
        }else {
            mBinding.tvApply.setText("立即报名");
            mBinding.tvApply.setBackgroundResource(R.drawable.red_color_change_radius_5);
        }

    }

    @Override
    public void onClick(View v) {
        long curClickTime = System.currentTimeMillis();
        if ((curClickTime - lastClickTime) >= MIN_CLICK_DELAY_TIME) {
            lastClickTime = curClickTime;
            switch (v.getId()) {
                case R.id.tv_detail:
                    if (clickListener != null) {
                        clickListener.detail(mEntity);
                    }
                    break;
                case R.id.tv_msg:

                    if (clickListener != null) {
                        clickListener.toIm(mEntity);
                    }
                    break;
                case R.id.tv_follow:
                    if (clickListener != null) {
                        clickListener.follow(mEntity);
                    }
                    break;
                case R.id.tv_apply_letter:

                    if (clickListener != null) {
                        clickListener.inquire(mEntity);
                    }
                    break;
                case R.id.tv_apply:
                    if (clickListener != null) {
                        clickListener.apply(mEntity);
                    }
                    break;
                case R.id.tv_navigation:
                    MapAppWindow  pickMapAppWindow = new MapAppWindow(mContext, 3);
                    pickMapAppWindow.setBgView(mBinding.grayLayout);
                    pickMapAppWindow.showPopwindowBottom(rootView);
                    pickMapAppWindow.setData(mEntity.getLatitude().doubleValue(), mEntity.getLongitude().doubleValue(), mEntity.getAddressAll());
                    pickMapAppWindow.setShowData(true, true, false);
                    dismiss();
                    pickMapAppWindow.setOnDismissListener(new OnDismissListener() {
                        @Override
                        public void onDismiss() {
                            if (clickListener != null) {
                                clickListener.showContentPop();
                            }
                        }
                    });
                    break;
                case R.id.tv_link_tel:
                    if (clickListener != null){
                        clickListener.toPhone(mEntity);
                    }
                    break;
                default:
                    break;
            }
        }
    }

    protected void callPhone(String phoneNum) {
        Intent intent = new Intent(Intent.ACTION_DIAL);
        Uri data = Uri.parse("tel:" + phoneNum);
        intent.setData(data);
        mContext.startActivity(intent);
    }

    public interface BtnClickListener {
        void follow(EmploymentEntity entity);

        void apply(EmploymentEntity entity);

        void detail(EmploymentEntity entity);

        void toIm(EmploymentEntity entity);

        void inquire(EmploymentEntity entity);

        void showContentPop();

        void toPhone(EmploymentEntity entity);
    }

}
